<?php

namespace app\admin\controller\erm;

use app\admin\{
    controller\Controllers,service\erm\Books as BooksService,model\erm\Books as BooksModel
};

class Books extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index( BooksService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\erm\Books.index');
        $list = $service->indexPaginate($get);
        $config = config('field.books');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'status' => $config['status']
        ]);
        return $this->fetch('erm/books/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $config = config('field.books');
        $sidebar = app('sidebar_service')->alls();
        $this->assign([
            'status' => $config['status'],
            'sidebar' => $sidebar
        ]);
        return $this->fetch('erm/books/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save( BooksService $service )
    {
        $post = $this->request->only(['cover','name','profiles','sid','sort','status'], 'post');
        $this->validate($post, 'app\admin\validate\erm\Books.create');
        $booksId = $service->create($post);
        if ( $booksId === false ) {
            return failed('新增失败');
        }
        return success();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(int $id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(BooksModel $books)
    {
        $config = config('field.books');
        $sidebar = app('sidebar_service')->alls();
        $this->assign([
            'status' => $config['status'],
            'sidebar' => $sidebar,
            'data' => $books
        ]);
        return $this->fetch('erm/books/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id, BooksService $service)
    {
        $put = $this->request->only(['cover','name','profiles','sid','sort','status'], 'put');
        $put['id'] = $id;
        $this->validate($put, 'app\admin\validate\erm\Books.update');
        $update = $service->save($id, $put);
        if ( $update === false ) {
            return failed('编辑失败');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, BooksService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\erm\Books.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 状态修改
     * @author zx
     * @date 2019-11-08
     * @return \think\Response
     */
    public function status(BooksService $service, BooksModel $model)
    {
        return $service->status($model);
    }

    /**
     * 检测侧边栏名称是否唯一
     * @author zx
     * @date 2019-12-17
     * @return \think\Response
     */
    public function only( BooksService $service )
    {
        $get = $this->request->only(['id','name'], 'get');
        $this->validate($get, 'app\admin\validate\erm\Books.only');
        $bool = $service->only($get);
        return json($bool);
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-12-19
     * @return \think\Response
     */
    public function destory(BooksService $books)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\erm\Books.destory');
        if ( $books->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
